package com.shaw.study.springx.transaction;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;

/**
 * Created by asus on 2018/5/15.
 */
public class ConnectionHolder {
    private Map<DataSource,Connection> connectionMap = new ConcurrentHashMap<>();

    public Connection getConnectionByDataSource(DataSource dataSource) throws SQLException {
        Connection connection = connectionMap.get(dataSource);
        if(connection==null){
            connection = dataSource.getConnection();
            connectionMap.put(dataSource,connection);
        }
        return connection;
    }
}
